package com.jdaw.datastructure.tree;

public class BinaryTreeDemo {
    public static void main(String[] args) {
        BinaryTree binaryTree=new BinaryTree();
        HeroNode one = new HeroNode("1", 1);
        HeroNode two=new HeroNode("2",2);
        HeroNode three=new HeroNode("3",3);
        HeroNode four=new HeroNode("4",4);
        HeroNode five=new HeroNode("5",5);
        one.setLeft(two);
        one.setRight(three);
        three.setLeft(five);
        three.setRight(four);
        binaryTree.setRoot(one);
        System.out.println("前序遍历");
        binaryTree.preOrder();
        System.out.println("删除节点");
        binaryTree.delNode(3);
        binaryTree.preOrder();


    }
}

//定义二叉树
class BinaryTree{
    private HeroNode root;

    public void setRoot(HeroNode root) {
        this.root = root;
    }
    //删除节点
    public void delNode(int no){
        if(root==null){
            System.out.println("空树不能删除");
        }else{
            if(root.getNo()==no){
                root=null;
            }else{
                root.delNode(no);
            }
        }
    }
    //前序遍历
    public void preOrder(){
        if(this.root!=null){
            this.root.preOrder();
        }
    }
    //中序遍历
    public void midOrder(){
        if(this.root!=null){
            this.root.midOrder();
        }
    }
    //后序遍历
    public void postOrder(){
        if(this.root!=null){
            this.root.postOrder();
        }
    }

    //前序查找
    public HeroNode preSearch(int no){
        if (root != null) {
            return root.preSearch(no);
        } else{
            return null;
        }
    }
    //中序查找
    public HeroNode midSearch(int no){
        if (root != null) {
            return root.midSearch(no);
        } else{
            return null;
        }
    }
    //后序查找
    public HeroNode postSearch(int no){
        if (root != null) {
            return root.postSearch(no);
        } else{
            return null;
        }
    }
}

class HeroNode{
    private String name;
    private int no;
    private HeroNode left;
    private HeroNode right;

    public HeroNode(String name, int no) {
        this.name = name;
        this.no = no;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public HeroNode getLeft() {
        return left;
    }

    public void setLeft(HeroNode left) {
        this.left = left;
    }

    public HeroNode getRight() {
        return right;
    }

    public void setRight(HeroNode right) {
        this.right = right;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "name='" + name + '\'' +
                ", no=" + no +
                '}';
    }

    //删除节点
    public void delNode(int no){
        if(this.left!=null&&this.left.no==no){
            this.left=null;
            return;
        }
        if(this.right!=null&&this.right.no==no){
            this.right=null;
            return;
        }
        if(this.left!=null){
            this.left.delNode(no);
        }
        if(this.right!=null){
            this.right.delNode(no);
        }
    }

    //前序遍历
    public void preOrder(){
        System.out.println(this);
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
    }
    //前序查找
    public HeroNode preSearch(int no){

        if(this.no==no){
            return this;
        }
        HeroNode resNode=null;
        if (this.left != null) {
            resNode=this.left.preSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if (this.right != null) {
            resNode=this.right.preSearch(no);
        }
        return resNode;
    }
    //中序遍历
    public void midOrder(){
        if (this.left != null) {
            this.left.midOrder();
        }
        System.out.println(this);
        if (this.right != null) {
            this.right.midOrder();
        }
    }
    //中序查找
    public HeroNode midSearch(int no){
        HeroNode resNode=null;
        if (this.left != null) {
            resNode=this.left.midSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if(no==this.no){
            return this;
        }
        if (this.right != null) {
            resNode=this.right.midSearch(no);
        }
        return resNode;
    }
    //后序遍历
    public void postOrder(){
        if (this.left != null) {
            this.left.preOrder();
        }
        if (this.right != null) {
            this.right.preOrder();
        }
        System.out.println(this);
    }
    //后序查找
    public HeroNode postSearch(int no){
        HeroNode resNode=null;
        if (this.left != null) {
            resNode=this.left.postSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if (this.right != null) {
            resNode=this.right.postSearch(no);
        }
        if(resNode!=null){
            return resNode;
        }
        if(this.no==no){
            return this;
        }
        return resNode;
    }
}
